package com.bootcamp.s2_0922.ds.Exercise._0930_Ex;

import java.util.HashMap;

/**
 * @program: myClassCode
 * @description:
 * @author: Keyu Li
 * @create: 2021-09-30 21:11
 **/

public class Test_03 {
    public static void main(String[] args) {

        String[] words = {"cccccccccccccat", "bt", "hhhhhhhhat", "tree"};
        String chars = "atach";
        char[] chars1 = chars.toCharArray();
        int charSize = 0;
        for (int i = 0; i < words.length; i++) {
            //将数组中的每一个字符串装进HashMap
            HashMap<Character, Integer> charMap = getCharMap(words[i]);
            //遍历判断数组
            for (int j = 0; j < chars1.length; j++) {
                //当数组中含有所有字符时,HashMap删除该字符;
                if (charMap.containsKey(chars1[j])) {
                    charMap.remove(chars1[j]);
                }
                //当HashMap为空时,已被删除完毕,该字符为好,将字符数增加该字符的长度
                if (charMap.isEmpty()) {
                    charSize += words[i].length();
                    break;
                }
            }
        }
        System.out.println(charSize);


    }

    private static HashMap<Character, Integer> getCharMap(String chars) {
        HashMap<Character, Integer> map = new HashMap<>();
        char[] ch1 = chars.toCharArray();
        for (int i = 0; i < ch1.length; i++) {
            map.put(ch1[i], i);
        }
        return map;
    }
}
